<?php
require_once 'common/init.php';
require_once 'config/conf.inc.php';

function main()
{
	global $tpl;
	$action = $_POST['act'];
	$is_register = true;
	$is_regok = false;
	$is_regerr = false;
	
	$login_user = LoginManager::get_login_user();
	if (empty($login_user))
	{
		$tpl->assign("login_status", LOGINSTATUS_OFF);
	}
	else
	{
		$tpl->assign("login_status", LOGINSTATUS_ON);
		$tpl->assign("login_user", $login_user);
	}
	
	if ($action == 'act_register')
	{
		$ret = add_user($_POST, $err);
		if ($ret)
		{
			$ret = $tpl->display('regsuccess.html');
			return;
		}
		else
		{
			$is_regerr = true;
			$is_register = false;
			echo "err=$err";
			//$tpl->assign("error_msg", $err);
		}
	}
	$ret = $tpl->display('register.html');	
}

function add_user($post, &$err)
{
	$user = trim($post['username']);
	$pass = trim($post['password']);
	$email = trim($post['email']);
	$err = "";
	
	$ret = AccountManager::check_user_account($user, $email, $err);
	//if error happen
	if(!empty($err))	return false;
	//if user exist
	if ($ret)	return false;
	
	$ret = AccountManager::insert_user_account($user, $pass, $email, USERTYPE_OWN, $err);
	//if error happen
	if(!empty($err))	return false;
	
	$reg_code = AccountManager::get_register_code($user, $email);
	$ret = AccountManager::send_register_mail($user, $email, $reg_code, $err);
	//if error happen
	if(!empty($err))	return false;
	
	return true;
}

main();


?>